package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.model.entity.NeedDetailsEntity;
import com.sgb.kjwl.model.entity.enterprise.NeedDetailSignUpEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.base.model.entity.BaseEntity;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class NeedListDetailModel extends AndroidViewModel {

    public MutableLiveData<String> Toast_until = new MutableLiveData<>();
    public MutableLiveData<Integer> ShowLoading = new MutableLiveData<>();
    public MutableLiveData<NeedDetailsEntity> date_detail = new MutableLiveData<>();//详情
    public String mitemid;
    public String[] titleList = {"需求信息","报价要求","报价单位"};
    public MutableLiveData<List<NeedDetailSignUpEntity>> list_signup = new MutableLiveData<>();

    public NeedListDetailModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void setMitemid(String id){
        this.mitemid = id;
    }

    public void getDetails() {
        ShowLoading.postValue(1);
        APIManager.getInstance().getNeedAPI().getDetails(mitemid).enqueue(new Callback<BaseEntity<NeedDetailsEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<NeedDetailsEntity>> call, Response<BaseEntity<NeedDetailsEntity>> response) {
                ShowLoading.postValue(2);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    date_detail.postValue(response.body().getData());
                } else {
                    Toast_until.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<NeedDetailsEntity>> call, Throwable t) {
                ShowLoading.postValue(2);
                Toast_until.postValue( t.getMessage());

            }
        });
    }

    public void updateType(){
        ShowLoading.postValue(1);

        APIManager.getInstance().getNeedAPI().updateNeedMessageType(date_detail.getValue().id,date_detail.getValue().demandStatus.equals("1")?"-1":"1").enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                ShowLoading.postValue(2);
                if(response.body()!=null){
                    NeedDetailsEntity updata = date_detail.getValue();
                    updata.demandStatus="-1";
                    updata.setOfferStatus(3);
                    date_detail.postValue(updata);
                }

            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                ShowLoading.postValue(2);

            }
        });

    }


    public String getBjType(int offerStatus){
        String datta = "/";

        switch (offerStatus){
            case 3:
                datta = "已取消";
                break;
            case 1:
                datta = "报价中";
                break;
            case 2:
                datta = "已结束";
                break;
        }

        return datta;
    }





    //1商标 2专利 3版权 4 公司 5 服务 6 资产
    public String getneedType(int type){
        String data = "/";
        switch (type){
            case 1:
                data = "商标需求";
                break;
            case 2:
                data = "专利需求";
                break;
            case 3:
                data = "版权需求";
                break;
            case 4:
                data = "公司需求";
                break;
            case 5:
                data = "服务需求";
                break;
            case 6:
                data = "资产需求";
                break;
        }

        return data;
    }

    /**
     * 专利状态判定
     * @param mpatentStatus
     * @return
     */
    public String getStringPatentStatus(String mpatentStatus){

        if(TextUtils.isEmpty(mpatentStatus)){
            return "";
        }
        StringBuilder patentStatusStr = new StringBuilder();
        if (mpatentStatus.indexOf(",")>-1) {
            for (String patentStatus : mpatentStatus.split(",")) {
                if(patentStatus.equals("1")){
                    patentStatusStr.append("授权已下证、");
                }
                if(patentStatus.equals("0")){
                    patentStatusStr.append("授权未下证、");
                }
            }
            patentStatusStr = new StringBuilder(patentStatusStr.substring(0, patentStatusStr.length() - 1));
        } else {
            patentStatusStr = new StringBuilder(mpatentStatus.contains("0") ? "授权未下证" :mpatentStatus.contains("1")? "授权已下证":"");
        }

        return patentStatusStr.toString();
    }


    /**
     * 授权方式
     * @param mcopyrightAuthorization
     * @return
     */
    public String getStringcopyrightAuthorization(String mcopyrightAuthorization){
        if(TextUtils.isEmpty(mcopyrightAuthorization)){
            return "";
        }
        StringBuilder copyrightAuthorizationStr = new StringBuilder();
        if (mcopyrightAuthorization.indexOf(",")>-1) {
            for (String copyrightAuthorization : mcopyrightAuthorization.split(",")) {
                copyrightAuthorizationStr.append(copyrightAuthorization.contains("1") ? "独家授权、" : "非独家授权、");
            }
            copyrightAuthorizationStr = new StringBuilder(copyrightAuthorizationStr.substring(0, copyrightAuthorizationStr.length() - 1));
        } else {
            copyrightAuthorizationStr = new StringBuilder(mcopyrightAuthorization.contains("1") ? "独家授权" : "非独家授权");
        }

        return copyrightAuthorizationStr.toString();
    }

    /**
     * 服务分类
     * @param class_type1
     * @param class_type2
     * @param class_type3
     * @return
     */
    public String getStringServiceType(String class_type1,String class_type2,String class_type3){
        String service_Type = "/";
        if (!TextUtils.isEmpty(class_type1)) {
            service_Type = class_type1;
        }
        if (!TextUtils.isEmpty(class_type2)) {
            service_Type = service_Type + "-" + class_type2;
        }
        if (!TextUtils.isEmpty(class_type3)) {
            service_Type = service_Type + "-" + class_type3;
        }
        return service_Type;
    }



    public void getsignuplist(String id){

        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getSignUpList(id,1+"",4).enqueue(new Callback<BaseEntity<ListResponse<List<NeedDetailSignUpEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<NeedDetailSignUpEntity>>>> call, Response<BaseEntity<ListResponse<List<NeedDetailSignUpEntity>>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if(response.body().getData().getList()!=null&&response.body().getData().getList().size()>0){
                        list_signup.postValue(response.body().getData().getList());
                    }
                } else {
                    Toast_until.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<NeedDetailSignUpEntity>>>> call, Throwable t) {
                Toast_until.postValue(t.getMessage());
            }
        });

    }

}



